package com.guli.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.common.handler.EduException;
import com.guli.edu.client.VodClient;
import com.guli.edu.entity.EduCourse;
import com.guli.edu.entity.EduVideo;
import com.guli.edu.entity.form.VideoInfoForm;
import com.guli.edu.mapper.EduVideoMapper;
import com.guli.edu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Daniel
 * @since 2019-03-29
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    @Autowired
    private VodClient vodClient;

    @Override
    public boolean getVideoByChapterId(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0 ;
    }

    @Override
    public boolean saveVideo(VideoInfoForm videoInfoForm) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoInfoForm,eduVideo);
        //调用方法实现添加
        int count  = baseMapper.insert(eduVideo);
        return count >0 ;
    }

    @Override
    public VideoInfoForm getVideoInfo(String id) {
        EduVideo eduVideo = baseMapper.selectById(id);
        if (eduVideo ==null) {
            throw new EduException(20001,"获取小节信息失败") ;
        }
        //把我们查询出来的eduvideo信息转换出来
        VideoInfoForm videoInfoForm = new VideoInfoForm();
        BeanUtils.copyProperties(eduVideo,videoInfoForm);

        return videoInfoForm;
    }

    @Override
    public boolean updateVideo(VideoInfoForm videoInfoForm) {
        EduVideo eduVideo = new EduVideo() ;
        BeanUtils.copyProperties(videoInfoForm,eduVideo);
        int count = baseMapper.updateById(eduVideo);
        return count >0 ;
    }

    @Override
    public boolean deleteVideoById(String id) {
        EduVideo eduVideo = baseMapper.selectById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            vodClient.removeVideo(videoSourceId);
        }
        Integer result = baseMapper.deleteById(id);
        return null != result && result > 0;
    }
    @Override
    public boolean removeCourseId(String courseId) {
        //根据课程id查询所有的视频id
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(queryWrapper);

        //得到所有视频的云端原始视频id
        List<String> videoSourceList = new ArrayList<>();
        for (EduVideo eduVideo : videoList) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {
                 videoSourceList.add(videoSourceId);
            }
        }
        //调用vod服务删除远程视频
        if (videoSourceList.size()>0) {
            vodClient.removeVideoList(videoSourceList);
        }

        //删除video表的记录
        QueryWrapper<EduVideo> queryWrapper1  = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        Integer count = baseMapper.delete(queryWrapper1);

        return null != count && count > 0;
    }
}
